import React from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import App from '../App';
import AdminPage from '../pages/adminPage';
import StudentPage from '../pages/studentPage';
import SysAdminPage from '../pages/sysAdminPage';
import CreateClub from '../pages/companents/createClubs';
import ClubList from '../pages/companents/clubList';

import JoinClub from '../pages/companents/student/joinClub';
import ManageClub from '../pages/companents/student/manageClub';
import MyClub from '../pages/companents/student/myClub';
import JoinActivityPage from '../pages/companents/activity/joinActivityPage';
import AnalyticsTable from '../pages/companents/analsyTable';
import Prize from '../pages/companents/Prizes/PrizePage';
import PrizeList from '../pages/companents/Prizes/PrizeList';
import StuPrizeList from '../pages/companents/Prizes/StuPrizeList';
import EditPrize from '../pages/companents/Prizes/EditPrize'

interface PrivateRouteProps {
  children: React.ReactNode;
  allowedRoles?: string[];  // 可选参数，如果不传则只需要登录即可访问
}

// 路由守卫组件
const PrivateRoute: React.FC<PrivateRouteProps> = ({ children, allowedRoles }) => {
  const userType = localStorage.getItem('userType')?.toLowerCase(); // 统一转为小写
  const userInfo = localStorage.getItem('userInfo');

  if (!userInfo) {
    // 如果没有登录信息，重定向到登录页
    return <Navigate to="/" replace />;
  }

  // 如果指定了allowedRoles且用户角色不在允许的角色列表中，重定向到无权限页
  if (allowedRoles && !allowedRoles.some(role => role.toLowerCase() === userType)) {
    return <Navigate to="/" replace />;  // 这里跳转回首页，您也可以创建专门的403页面
  }

  return <>{children}</>;
};

const Router = () => {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<App />} />
        <Route path="/admin" element={<PrivateRoute><AdminPage /></PrivateRoute>}/>
        <Route path="/student/*" element={<PrivateRoute><StudentPage /></PrivateRoute>} />
        <Route path="/joinClub" element={<PrivateRoute><JoinClub /></PrivateRoute>} />
        <Route path="/manageClub" element={<PrivateRoute><ManageClub /></PrivateRoute>} />
        <Route path="/myClub" element={<PrivateRoute><MyClub /></PrivateRoute>} />
        <Route path="/sysAdmin" element={<PrivateRoute><SysAdminPage /></PrivateRoute>}/>
        <Route path="/createClub" element={<PrivateRoute allowedRoles={['systemAdmin']}><CreateClub /></PrivateRoute>}/>
        <Route path="/allClubs" element={<PrivateRoute allowedRoles={['systemAdmin']}><ClubList /></PrivateRoute>}/>
        <Route path="/deleteClub" element={<PrivateRoute allowedRoles={['systemAdmin']}><ClubList /></PrivateRoute>}/>
        
        {/* Activity Routes */}
        <Route path="/activities/list" element={<PrivateRoute><JoinActivityPage /></PrivateRoute>} />
        <Route path="/activities/my" element={<PrivateRoute><div>我的参与页面（待实现）</div></PrivateRoute>} />
        <Route path="/activities/points" element={<PrivateRoute><div>我的积分页面（待实现）</div></PrivateRoute>} />
        <Route path="/analytics" element={<PrivateRoute><AnalyticsTable /></PrivateRoute>} />
        <Route path="/prizes" element={<PrivateRoute><Prize /></PrivateRoute>} />
        <Route path="/PrizeList" element={<PrivateRoute><PrizeList /></PrivateRoute>} />
        <Route path="/StuPrizeList" element={<PrivateRoute><StuPrizeList /></PrivateRoute>} />
        <Route path="/EditPrizes/:id" element={<PrivateRoute><EditPrize /></PrivateRoute>} />
      </Routes>
    </BrowserRouter>
  );
};

export default Router;